package com.example.administrator.meishi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.meishi.utils.Activityshow;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Show extends AppCompatActivity {
    //展示界面
    private String next;
    private ListView lv;
    private List<String> list;
    private List<String> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //用来展示收藏数据
        final SharedPreferences meishi = getSharedPreferences("meishi", MODE_PRIVATE);
        Map<String, Set<String>> map = (Map<String, Set<String>>) meishi.getAll();
        data = new ArrayList<>();
        Collection<Set<String>> values = map.values();
        Iterator<Set<String>> iterator = values.iterator();
        list = new ArrayList<>();
        while (iterator.hasNext()){
            Set<String> set = iterator.next();
            Iterator<String> iterator1 = set.iterator();
            Map<String,String> map1 = new HashMap<>();
            while (iterator1.hasNext()){
                next=iterator1.next();
                if (isContainsChinese(next)){
                    list.add(next);
                }else {
                    data.add(next);
                }
            }

        }
        //将数据展示在listview上
        lv = (ListView) findViewById(R.id.lv_show);
        final ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);
        //查询全部的数据将数据展示到listview上
        //SDUtil.readData();23
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(Show.this,Activityshow.class);
                String url = data.get(position);
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                list.remove(position);//删除listview里的数据
                adapter.notifyDataSetChanged();//更新数据
                return true;
            }
        });
    }

    private boolean isContainsChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find()){
            flg = true;
        }
        return flg;
    }
}
